﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    class pacman
    {
        public Rectangle Rectangle { get; set; }
        private const int step = 50;
        private const int GameWidth = 1000;
        private const int GameHeight = 600;
        public pacman(Rectangle rec)
        {
            Rectangle = rec;

        }
        public void Draw(Graphics g)
        {
            g.DrawImage(Properties.Resources.pacman, Rectangle);
        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            int newX = Rectangle.X;
            int newY = Rectangle.Y;
            if (key == "A")
            {
                newX = Rectangle.X - step;
                if (newX < 0)
                    newX = 0;
            }
            if (key == "D")
            {
                newX = Rectangle.X + step;
                if (newX + Rectangle.Width > GameWidth)
                    newX = GameWidth - Rectangle.Width;
            }
            if (key == "W")
            {
                newY = Rectangle.Y - step;
                if (newY < 0)
                    newY = 0;
            }
            if (key == "S")
            {
                newY = Rectangle.Y + step;
                if (newY + Rectangle.Height > GameHeight)
                    newY = GameHeight - Rectangle.Height;
            }
            Rectangle = new Rectangle(newX,
                                      newY,
                                      Rectangle.Width,
                                      Rectangle.Height);
        }
    }
}
